<?php


namespace app\services;


use app\controllers\pay\Sign;
use app\util\Request;
use app\models\user\User;

class ScoreService
{
    public  static  function generateRandomNumber($length) {
        $min = pow(10, $length - 1);
        $max = pow(10, $length) - 1;
        return mt_rand($min, $max);
    }

    public static function auth($user_id)
    {
        $url = 'https://yxzm.ykwhfgs999.com.cn/';
        $url .= 'api/User/identity_api';

        $key = 'e4205872fa5a2a4dd1f111973edeb109';


        $user = User::findOne(['id' => $user_id]);

        // 18位
        // var_dump(strlen('142330199805216211'));exit;
        // $i = self::generateRandomNumber(18 - strlen($user->id));
        // $i = $user_id . '' . $i;

        $name = '牛志强';
        if ($user->nickname) {
            // $name = base64_decode($user->nickname);
        }

        $data = [
            'identity_name' => $name,
            // 'identity_card' => $i,
            'identity_card' => '142330199805216211',
            'mobile' => $user->mobile,
        ];

        $x = $data;
        ksort($x);

        $sign = Sign::signature2($x);
        $sign .= 'key=' . $key;

        $data['sign'] = strtoupper(md5($sign));

        // var_dump($sign, $data);

        $res = Request::post($url, $data);
        // var_dump($res);exit;
    }

    public static function score($user_id, $score, $type = '1')
    {
        $url = 'https://yxzm.ykwhfgs999.com.cn/';
        $url .= 'api/User/youdou_api';

        $key = 'e4205872fa5a2a4dd1f111973edeb109';

        $user = User::findOne(['id' => $user_id]);

        $d = [
            // 'identity_name' => '牛志强',
            // 'identity_card' => '142330199805216211',
        ];

        $data = [
            'mobile' => $user->mobile,
            'youdou' => '' . $score,
            'change_type' => $type,
        ];

        $data = array_merge($d, $data);
        // ksort($data);

        $x = $data;
        ksort($x);


        $sign = Sign::signature2($x);
        $sign .= 'key=' . $key;

        $data['sign'] = strtoupper(md5($sign));

        // var_dump($data, $sign);

        $res = Request::post($url, $data);
        // var_dump($res);exit;

        return true;
    }
}